/**
 * 创建者：luyiming
 * 该组件用于：
 * 创建时间： 2020-03-08 09:50:31
 * @param 参数说明
 * @emit 事件使用说明
 */
import {
	cloneDeep
} from "lodash";
export default {
	name: 'ReportTable_cmp',
	props: {
		// 这是示例数据的通途
		example: {
			type: Array,
			default: () => []
		},
		sourceTable: {
			type: Array,
			default: () => []
		},
		reportTitle: {
			type: String,
			default: () => ''
		},
		isReadOnly: {
			type: Boolean,
			default: () => false,
		},
		ruleForm: {
			type: Object,
			default: () => {}
		},
	},
	// 数据
	data() {
		return {
			sourceForm: {

			}
		}
	},
	// 监听
	watch: {

	},
	// Vue实例初始化完成
	created() {

	},
	// Vue挂载完成
	mounted() {
	},
	// Vue实例销毁前
	beforeDestroy() {
		// this.sourceForm = {}
	},
	// 挂载的方法
	methods: {
		/**
		 * 方法描述
		 */
		exampleFunc() {
			return void 0
		},
		submitTable() {
			// if (Object.keys(this.ruleForm).length) {
			// 	const valid = this.validForm(this.ruleForm, this.sourceForm)
			// 	if (!valid) return false
			// }
			return this.sourceForm
		},
		validForm(data, ruleForm) {
			let valid = true
			// 循环校验内容
			return new Promise((res,rej) => {
                for (const key in ruleForm) {
                    if (ruleForm.hasOwnProperty(key)) {
                        // 循环校验条件
                        for (const iterator of ruleForm[key]) {
                            if (iterator.required && !data[key]) {
                                this.$message({
                                    message: iterator.message,
                                    type: 'warning'
                                })
                                valid = false
                                break
                            }
                            if (iterator.validator && !iterator.validator(data[key]) && data[key]) {
                                this.$message({
                                    message: iterator.message,
                                    type: 'warning'
                                })
                                valid = false
                                break
                            }
                        }
                    }
                    if (!valid) {
                        break
                    }
                }
                res(valid)
            })

		},
		clearForm() {
			Object.keys(this.sourceForm).forEach(itemKey => {
				this.sourceForm[itemKey] = ""
			})
		},
		setDefaultValue(params = {}) {
			// Object.assign(this.sourceForm, params)
			Object.keys(params).forEach(itemKey => {
				this.$set(this.sourceForm, itemKey, params[itemKey])
			})
		},
		deleteKeys(keys){
			// debugger
			keys.forEach(itemKey => {
				delete this.sourceForm[itemKey]
			})
		},
		addEvent(col) {
			this.sourceForm[col.key] = this.sourceForm[col.key] - 0 + 1;
		},
		delEvent(col, i) {
			this.sourceForm[col.key] = this.sourceForm[col.key] - 1;
			const cloneData = cloneDeep(this.sourceForm);
			const codeKeys = Object.keys(cloneData).filter(itemKey => /^code__/.test(itemKey)).map(item => item.split('__')[1]);
			const max = Math.max(...codeKeys);
			for (let j = 0; j < max; j++) {
				if (j >= i) {
					this.sourceForm['code__' + j] = cloneData['code__' + (j + 1)]
				}
			}
			delete this.sourceForm['code__' + max]
		}
	},
}
